package se.webbzon.boltzmann.game.object.player;

import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.boltzmann.game.character.CharacterException;
import se.webbzon.boltzmann.game.environment.GameEnvironment;
import se.webbzon.boltzmann.object.factory.InstanceFactory;
import se.webbzon.boltzmann.object.unknown.Unknown;
import se.webbzon.boltzmann.player.Player;
import se.webbzon.oschi01.worldobject.WorldObject;

public class AbstractPlayerCharacterFactory extends InstanceFactory {
	
	/*============================================================
	Constructors
	============================================================*/
	
	public AbstractPlayerCharacterFactory() {
		super("abstractPlayerCharacter");
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public WorldObject makeNew(AbstractEnvironment body) {
		if (body instanceof GameEnvironment) {
			GameEnvironment env = (GameEnvironment) body;
			
			// Find the first player in the game environment
			Player player = null;
			for (Player p : env.getPlayers()) {
				player = p;
				break;
			}

			// Return a new game player character for the player
			if (player == null)
				return new Unknown();
			else try {
				return player.getCharacter().makePlayerCharacter(player);
			} catch (CharacterException e) {
				return new Unknown();
			}
		} else
			return new Unknown();
	}

}
